package com.example.x6.configurationmaintenance.Module;

import android.os.Build;
import android.util.Log;
import com.example.x6.configurationmaintenance.Common.BuildModel;
import com.example.x6.configurationmaintenance.Interface.BootanimationListener.OnBootanimationFindListener;
import com.example.x6.configurationmaintenance.Interface.BootanimationListener.OnBootanimationReplaceListener;
import com.example.x6.configurationmaintenance.Interface.BootanimationListener.OnBootanimationResetFinishListener;
import com.example.x6.configurationmaintenance.Utils.SuCommand;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class BootanimationManager {
    private final String TAG = getClass().getSimpleName();
    private OnBootanimationFindListener bootanimationFindListener;
    private OnBootanimationReplaceListener bootanimationReplaceListener;
    private OnBootanimationResetFinishListener bootanimationResetFinishListener;

    public void setBootanimationFindListener(OnBootanimationFindListener bootanimationFindListener2) {
        this.bootanimationFindListener = bootanimationFindListener2;
        startSearch();
    }

    private void startSearch() {
        new Thread(new Runnable() {
            /* class com.example.x6.configurationmaintenance.Module.BootanimationManager.AnonymousClass1 */

            public void run() {
                int num = 0;
                DataOutputStream dos = null;
                DataInputStream dis = null;
                try {
                    Process p = Runtime.getRuntime().exec("su");
                    DataOutputStream dos2 = new DataOutputStream(p.getOutputStream());
                    DataInputStream dis2 = new DataInputStream(p.getInputStream());
                    dos2.writeBytes("busybox find / -name bootanimation.zip" + "\n");
                    dos2.flush();
                    dos2.writeBytes("exit\n");
                    dos2.flush();
                    while (true) {
                        String line = dis2.readLine();
                        if (line == null) {
                            break;
                        }
                        BootanimationManager.this.bootanimationFindListener.bootanimationFind(line);
                        num++;
                    }
                    p.waitFor();
                    if (num == 0) {
                        BootanimationManager.this.bootanimationFindListener.bootanimationFindFinished(true);
                    } else {
                        BootanimationManager.this.bootanimationResetFinishListener.bootanimationResetFinished(false);
                    }
                    if (dos2 != null) {
                        try {
                            dos2.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (dis2 != null) {
                        try {
                            dis2.close();
                        } catch (IOException e2) {
                            e2.printStackTrace();
                        }
                    }
                } catch (Exception e3) {
                    e3.printStackTrace();
                    if (0 != 0) {
                        try {
                            dos.close();
                        } catch (IOException e4) {
                            e4.printStackTrace();
                        }
                    }
                    if (0 != 0) {
                        try {
                            dis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                } catch (Throwable th) {
                    if (0 != 0) {
                        try {
                            dos.close();
                        } catch (IOException e5) {
                            e5.printStackTrace();
                        }
                    }
                    if (0 != 0) {
                        try {
                            dis.close();
                        } catch (IOException e6) {
                            e6.printStackTrace();
                        }
                    }
                    throw th;
                }
            }
        }).start();
    }

    public void setBootanimationResetFinishListener(OnBootanimationResetFinishListener bootanimationResetFinishListener2) {
        this.bootanimationResetFinishListener = bootanimationResetFinishListener2;
        resetBootanimation();
    }

    private void resetBootanimation() {
        new Thread(new Runnable() {
            /* class com.example.x6.configurationmaintenance.Module.BootanimationManager.AnonymousClass2 */

            public void run() {
                SuCommand suCommand = new SuCommand();
                if (suCommand.execRootCmdSilent("mount -o rw,remount /system") != 0) {
                    Log.e(BootanimationManager.this.TAG, "挂在系统失败");
                    BootanimationManager.this.bootanimationResetFinishListener.bootanimationResetFinished(false);
                } else if (suCommand.execRootCmdSilent("rm /system/media/bootanimation.zip") != 0) {
                    Log.e(BootanimationManager.this.TAG, "还原失败");
                    BootanimationManager.this.bootanimationResetFinishListener.bootanimationResetFinished(false);
                } else {
                    suCommand.execRootCmdSilent("sync");
                    BootanimationManager.this.bootanimationResetFinishListener.bootanimationResetFinished(true);
                }
            }
        }).start();
    }

    public void setBootanimationReplaceListener(OnBootanimationReplaceListener bootanimationReplaceListener2, String path) {
        this.bootanimationReplaceListener = bootanimationReplaceListener2;
        startReplace(path);
    }

    private void startReplace(final String path) {
        new Thread(new Runnable() {
            /* class com.example.x6.configurationmaintenance.Module.BootanimationManager.AnonymousClass3 */

            public void run() {
                if (path == null) {
                    Log.e(BootanimationManager.this.TAG, "替换路径为空");
                    BootanimationManager.this.bootanimationReplaceListener.bootanimationRelpaced(false);
                    return;
                }
                SuCommand suCommand = new SuCommand();
                if (suCommand.execRootCmdSilent("mount -o rw,remount /system") != 0) {
                    Log.e(BootanimationManager.this.TAG, "挂载失败");
                    BootanimationManager.this.bootanimationReplaceListener.bootanimationRelpaced(false);
                    return;
                }
                if (suCommand.execRootCmdSilent("cp " + path + " /system/media/") != 0) {
                    Log.e(BootanimationManager.this.TAG, "拷贝失败");
                    BootanimationManager.this.bootanimationReplaceListener.bootanimationRelpaced(false);
                } else if (suCommand.execRootCmdSilent("chmod 777 /system/media/bootanimation.zip") != 0) {
                    Log.e(BootanimationManager.this.TAG, "权限给与失败");
                    BootanimationManager.this.bootanimationReplaceListener.bootanimationRelpaced(false);
                } else if (Build.MODEL.equals(BuildModel.SC20) || suCommand.execRootCmdSilent("mount -r -o remount /system") == 0) {
                    suCommand.execRootCmdSilent("sync");
                    BootanimationManager.this.bootanimationReplaceListener.bootanimationRelpaced(true);
                } else {
                    Log.e(BootanimationManager.this.TAG, "挂载失败");
                    BootanimationManager.this.bootanimationReplaceListener.bootanimationRelpaced(false);
                }
            }
        }).start();
    }
}
